package com.msh.starter.id.generate.config;

import com.msh.frame.client.interfaces.IClassLoadLog;
import com.msh.frame.interfaces.IIdGenerateServiceId;
import com.msh.frame.interfaces.IdGenerateable;
import com.msh.starter.id.generate.enums.IdGenerateLengthEnum;
import com.msh.starter.id.generate.enums.IdGenerateTypeEnum;
import com.msh.starter.id.generate.instance.DateIdGenerate;
import com.msh.starter.id.generate.instance.IdGenerate;
import com.msh.starter.id.generate.instance.OneServiceIdGenerateServiceId;
import com.msh.starter.id.generate.util.IdGenerateUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnMissingBean(IdGenerateable.class)
public class IdGenerateAutoConfiguration implements IClassLoadLog {
  private boolean loadDefaultIdGenerateServiceId;
  private boolean loadDateIdGenerate;
  private boolean loadIdGenerate;


  @Bean
  @ConditionalOnMissingBean(IIdGenerateServiceId.class)
  public OneServiceIdGenerateServiceId defaultIdGenerateServiceId(){
    loadDefaultIdGenerateServiceId = true;
    return new OneServiceIdGenerateServiceId();
  }

  @Bean
  @ConditionalOnProperty(name = "msh.starter.idGenerate.date-id.enable", havingValue = "true")
  public IdGenerateable defaultDateIdGenerate(IIdGenerateServiceId idGenerateServiceId){
    loadDateIdGenerate = true;
    return IdGenerateUtil.buildIdGenerate(idGenerateServiceId, IdGenerateTypeEnum.DATE.getCode(), IdGenerateLengthEnum.ID_LENGTH_MEDIUM.getCode());
  }

  @Bean
  @ConditionalOnProperty(name = "msh.starter.idGenerate.defalue.enable", havingValue = "true")
  public IdGenerateable defaultIdGenerate(IIdGenerateServiceId idGenerateServiceId){
    loadIdGenerate = true;
    return IdGenerateUtil.buildIdGenerate(idGenerateServiceId, IdGenerateTypeEnum.DEFAULT.getCode(), IdGenerateLengthEnum.ID_LENGTH_MEDIUM.getCode());
  }

  public static void main(String[] args) {
    IdGenerate dateIdGenerate = new IdGenerate();
    for (int i = 0; i <5000; i++){
      System.out.println(dateIdGenerate.getUniqueId());

    }
  }

  @Override
  public String logInfo() {
    if(!loadDefaultIdGenerateServiceId && !loadDateIdGenerate && !loadIdGenerate){
      return null;
    }
    StringBuilder sb = new StringBuilder()
        .append("----执行类 ")
        .append(IdGenerateAutoConfiguration.class)
        .append("\r\n");
    if(loadDefaultIdGenerateServiceId){
      sb.append("--------加载类 ")
              .append(OneServiceIdGenerateServiceId.class)
              .append("(DefaultIdGenerateServiceId)\r\n");
    }
    if(loadDateIdGenerate){
      sb.append("--------加载类 ")
              .append(DateIdGenerate.class)
              .append("(DateIdGenerate)\r\n");
    }
    if(loadIdGenerate){
      sb.append("--------加载类 ")
          .append(IdGenerate.class)
          .append("(IdGenerate)\r\n");
    }
    return sb.toString();
  }
}
